package com.ease.business.importing.vo;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.exception.ExceptionUtils;

import com.ease.common.util.Assertion;
import com.ease.common.util.Detect;
import com.ease.model.Parameter;

/**
 * 每一行的validation信息
 * 
 * @author nathanleewei
 * 
 */
public class RowValidationParam implements Parameter {

	/**
	 *
	 */
	private static final long serialVersionUID = -7286677422998700836L;

	private boolean validationSuccess = true;

	private String validatedColumns;

	private int rowIndex;

	/**
	 * 每一列的信息
	 */
	private List<AttributeValidationParam> valueValidationParams = null;

	private void addValidationMessage(String validationLabel, int columnIndex, String failureMessage) {
		if (null == valueValidationParams) {
			valueValidationParams = new ArrayList<AttributeValidationParam>();
		}
		AttributeValidationParam valueValidationParam = new AttributeValidationParam();
		valueValidationParams.add(valueValidationParam);

		valueValidationParam.setValidationLabel(validationLabel);
		valueValidationParam.setColumnIndex(columnIndex);
		valueValidationParam.setMessage(failureMessage);
	}

	public String resultMessage() {
		if (Detect.notEmpty(valueValidationParams)) {
			StringBuilder message = new StringBuilder();
			for (AttributeValidationParam valueValidationParam : valueValidationParams) {
				message.append("\n");
				if (null != valueValidationParam.getValidationLabel()) {
					message.append(valueValidationParam.getValidationLabel());
				}
				message.append(valueValidationParam.getMessage()).append(",");
			}
			return message.toString();
		}
		return null;
	}

	public void addValidatedColumn(String validatedColumn) {
		if (Detect.notEmpty(validatedColumn)) {
			if (null == validatedColumns) {
				validatedColumns = ",";
			}
			validatedColumns = validatedColumns + validatedColumn + ",";
		}
	}

	public boolean isColumnValidated(String validatedColumn) {
		Assertion.notEmpty(validatedColumn, "validatedColumn not found");

		if (null != validatedColumns) {
			return (validatedColumns.indexOf(validatedColumn) >= 0);
		}
		return false;
	}

	public void clearValidatedColumn() {
		validatedColumns = null;
	}

	public boolean validate(String validationLabel, Throwable e) {
		// return validate(false, validationLabel, -1, ExceptionUtil.write(e));
		Throwable throwable = ExceptionUtils.getRootCause(e);
		if (null != throwable) {
			e = throwable;
		}
		return validate(false, validationLabel, -1, e.getMessage());// + "\n" + ExceptionUtils.getRootCauseMessage(e));
	}

	public boolean validate(boolean expression, String validationLabel, int columnIndex, String failureMessage) {
		if (false == expression) {
			validationSuccess = false;
			addValidationMessage(validationLabel, columnIndex, failureMessage);
		}
		return expression;
	}

	/**
	 * @return the rowIndex
	 */
	public int getRowIndex() {
		return rowIndex;
	}

	/**
	 * @param rowIndex
	 *            the rowIndex to set
	 */
	public void setRowIndex(int rowIndex) {
		this.rowIndex = rowIndex;
	}

	/**
	 * @return the valueValidationParams
	 */
	public List<AttributeValidationParam> getValueValidationParams() {
		return valueValidationParams;
	}

	/**
	 * @param valueValidationParams
	 *            the valueValidationParams to set
	 */
	public void setValueValidationParams(List<AttributeValidationParam> valueValidationParams) {
		this.valueValidationParams = valueValidationParams;
	}

	/**
	 * @return the validationSuccess
	 */
	public boolean isValidationSuccess() {
		return validationSuccess;
	}

	/**
	 * @param validationSuccess
	 *            the validationSuccess to set
	 */
	public void setValidationSuccess(boolean validationSuccess) {
		this.validationSuccess = validationSuccess;
	}

}
